package zuoye_3_24;
//在你生成的文件中，随机点名名字，在控制台打印一行


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Zuoye_1 {
	public static void main(String[] args) throws Exception {
		String xingStr="https://www.jimingbao.com/90981.html";
		String nvmingStr="http://www.haoming8.cn/mingzi/25026.html";
		String nanmingStr="http://www.haoming8.cn/mingzi/25024.html";
		
		String webCrawlerXing = webCrawler(xingStr);
		String webCrawlernvMing = webCrawler(nvmingStr);
		String webCrawlernanMing = webCrawler(nanmingStr);
//		System.out.println(webCrawlerXing);
		
		
		
		//通过正则表达式，把其中符号要求的数据获取出来
		ArrayList<String> dataXing = getData(webCrawlerXing,"(.、){2}");
		ArrayList<String> datanvMing = getData(webCrawlernvMing,"(..、){4}..");
		ArrayList<String> datananMing = getData(webCrawlernanMing,"(..、){4}..");

	//	System.out.println(dataXing);
	//	System.out.println(datanvMing);
	//	System.out.println(datananMing);
		//处理姓氏
		ArrayList<String> xingList=new ArrayList();
		getName(dataXing, xingList);
		//System.out.println(xingList);
		//处理女名字
		ArrayList<String> nvmingList=new ArrayList();
		getName(datanvMing, nvmingList);
	//	System.out.println(nvmingList);
		//处理男名字
		ArrayList<String> nanmingList=new ArrayList();
		getName(datananMing, nanmingList);
	//	System.out.println(nanmingList);
		//自由生成女名字
		ArrayList<String> nvinfos = getnvinfos(xingList,nvmingList,50,0);
		System.out.println(nvinfos);
		//自由生成男名字
		ArrayList<String> naninfos = getnvinfos(xingList,nanmingList,50,1);
		System.out.println(naninfos);
		//写出数据
		BufferedWriter bw=new BufferedWriter(new FileWriter("names1.txt"));
		for (String string : nvinfos) {
			bw.write(string);
			bw.newLine();
		}
		for (String string : naninfos) {
			bw.write(string);
			bw.newLine();
		}
		bw.close();
	}
	private static ArrayList<String> getnvinfos(ArrayList<String> xingList, ArrayList<String> mingList, int count, int 
			gender) {
		HashSet<String> set=new HashSet<>();
		Random r = new Random();
		int age = 0;
		String setGender = null;
		while(true){
			if(set.size()==count){
				break;
			}
			if(gender == 1) {
				age = r.nextInt(20,25);
				setGender = "男";
			}else if(gender == 0) {
				age = r.nextInt(18,22);
				setGender = "女";
			}
			//随机
			Collections.shuffle(mingList);
			Collections.shuffle(xingList);
			set.add(xingList.get(0)+mingList.get(0)+"-"+setGender+"-"+age+"-"+"1");
		}
		ArrayList<String> list=new ArrayList<>(set);
		return list;
	}
	
	public static ArrayList<String> getData(String str,String regex){
		ArrayList<String> list=new ArrayList<>();
		//按照正则表达式的规则，去获取数据
		Pattern pattern=Pattern.compile(regex);
		//按照pattern规则，到str中获取数据
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			String group = matcher.group();
//			System.out.println(group);
			list.add(group);
		}
		return list;
	}
	
	/*
	 * 作用：
	 *  从网络中爬取数据，把数据拼接成字符串返回
	 *  参数：网址
	 *  返回值：爬取到的所有的数据
	 */
	public static String webCrawler(String net) throws Exception{
		StringBuilder sb=new StringBuilder();
		//创建一个url对象
		URL url=new URL(net);
		//连接上这个网址 ，保证网络是畅通的
		URLConnection conn = url.openConnection();
		//读取数据
		InputStreamReader isr=new InputStreamReader(conn.getInputStream(),"utf-8");
		int ch;
		while((ch=isr.read())!=-1){
			sb.append((char)ch);
		}
		return sb.toString();
	}
	
	public static void getName(ArrayList<String> list1,ArrayList<String> list2) {
		for (String xing : list1) {
			String[] split = xing.split("、");
			for(int i=0;i<split.length;i++){
				list2.add(split[i]);
			}
		}
	}
}

